package src.main.java.org.collection.Set;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class Test2 {
    public static void main(String[] args) {
//        第一种方法：让自定义类实现Comparable接口，重写里面的compareTo方法来指定比较规则
        Set<Student> students = new TreeSet<>();
        students.add(new Student("张三",23,187));
        students.add(new Student("李四",22,184));
        students.add(new Student("王五",24,181));
        students.add(new Student("赵六",21,188));
        System.out.println(students);


//        第二种方法：调用TreeSet集合有参构造器，可以设置Comparator对象（比较器对象，用于指定比较规则），会就近选择此方法
        /*TreeSet<Student> student2 = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Double.compare(o1.getHeight(),o2.getHeight());
            }
        });*/
        TreeSet<Student> student2 = new TreeSet<>((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight()));

    }
}
